package com.sfh.dog.ui;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.sfh.dog.DogApplication;
import com.sfh.dog.R;
import com.sfh.dog.entity.Dog;
import com.sfh.dog.ui.base.BaseActivity;
import com.sfh.dog.util.StringUtils;
import com.sfh.dog.util.ViewUtil;

/**
 * 查看宠物信息
 * 
 * @author Swer
 * 
 */
public class LookDogActivity extends BaseActivity
{
    private ImageView ivDogPhoto;
    
    private TextView tvTitle, tvDogName, tvDogType1, tvDogType2, tvDogStatus, tvDogBirthday;
    
    private RadioButton rbMM, rbGG;
    
    private LinearLayout lySpecialty, lyLove, lyDisgusting;
    
    /** 宠物ID */
    private String mDogID;
    
    private boolean isLoad = true;
    
    public static final String Key = "DogID";
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.look_dog_layout);
        find();
        init();
    }
    
    private void find()
    {
        // 退出
        findViewById(R.id.ivLeft).setOnClickListener(new View.OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                finish();
            }
        });
        tvTitle = (TextView)findViewById(R.id.tvTitle);
        ivDogPhoto = (ImageView)findViewById(R.id.ivDogphoto);
        tvDogName = (TextView)findViewById(R.id.tvDogName);
        tvDogType1 = (TextView)findViewById(R.id.tvDogType1);
        tvDogType2 = (TextView)findViewById(R.id.tvDogType2);
        tvDogStatus = (TextView)findViewById(R.id.tvDogStatus);
        tvDogBirthday = (TextView)findViewById(R.id.tvDogBirthday);
        rbMM = (RadioButton)findViewById(R.id.rbDogMM);
        rbGG = (RadioButton)findViewById(R.id.rbDogGG);
        lySpecialty = (LinearLayout)findViewById(R.id.lySpecialty);
        lyLove = (LinearLayout)findViewById(R.id.lyLove);
        lyDisgusting = (LinearLayout)findViewById(R.id.lyDisgusting);
    }
    
    private void init()
    {
        mDogID = getIntent().getStringExtra(Key);
        tvTitle.setText("宠物详情");
    }
    
    @Override
    protected void onResume()
    {
        super.onResume();
        if (isLoad)
        {
            isLoad = false;
            BmobQuery<Dog> query = new BmobQuery<Dog>();
            query.addWhereEqualTo("", mDogID);
            query.findObjects(this, new FindListener<Dog>()
            {
                
                @Override
                public void onSuccess(List<Dog> arg0)
                {
                    if (arg0 != null && arg0.size() >0)
                    {
                        setDog(arg0.get(0));
                    }
                }
                
                @Override
                public void onError(int arg0, String arg1)
                {
                    
                }
            });
            
        }
    }
    
    private void setDog(Dog dog)
    {
        ImageLoader.getInstance().displayImage(dog.getImage(),
            ivDogPhoto,
            DogApplication.options);
        tvDogName.setText(dog.getName());
        tvDogType1.setText(dog.getTypeDog());
        tvDogType2.setText(dog.getTypeName());
        tvDogBirthday.setText(dog.getBirthday());
        tvDogStatus.setText(dog.getStatus());
        rbMM.setChecked(0 == dog.getSex());
        rbGG.setChecked(1 == dog.getSex());
        if (!StringUtils.isNullOrEmpty(dog.getSpecialty()))
        {
            ViewUtil.setLove(LookDogActivity.this, lySpecialty, dog.getSpecialty());
        }
        if (!StringUtils.isNullOrEmpty(dog.getLove()))
        {
            ViewUtil.setLove(LookDogActivity.this, lyLove, dog.getLove());
        }
        if (!StringUtils.isNullOrEmpty(dog.getDisgusting()))
        {
            ViewUtil.setLove(LookDogActivity.this, lyDisgusting, dog.getDisgusting());
        }
        
    }
    
}
